
#include "exynos_4412.h"

void Delay(unsigned int Time){
	while(Time--);
}

int main()
{
	unsigned int OldSec = 0, NewSec = 0;
	unsigned int AdcValue;

	/*设置ADC精度为12bit*/
	ADCCON = ADCCON | (1 << 16);
	/*使能ADC分频器*/
	ADCCON = ADCCON | (1 << 14);
	/*设置ADC分频值 ADC时钟频率=PLCK/(19+1)=5MHZ ADC转换频率=5MHZ/5=1MHZ*/
	ADCCON = ADCCON & (~(0xFF << 6)) | (19 << 6);
	/*关闭待机模式，使能正常模式*/
	ADCCON = ADCCON & (~(1 << 2));
	/*关闭通过读使能AD转换*/
	ADCCON = ADCCON & (~(1 << 1));
	/*选择转换通道，3通道*/
	ADCMUX = 3;

	//控制LED2
	GPX2.CON = GPX2.CON & (~(0xF << 28)) | (0x1 << 28);
	//控制LED3
	GPX1.CON = GPX1.CON | 1;
	//控制LED4
	GPF3.CON = GPF3.CON | (1 << 16);
	//控制LED5
	GPF3.CON = GPF3.CON | (1 << 20);

	/*使能RTC控制*/
	RTCCON = RTCCON | 1;
	/*校准时间信息 表达初始时间：2023-12-31 星期日 23:59:50 这里4412的周几和当日字段写反了*/
	RTC.BCDYEAR = 0x023;
	RTC.BCDMON  = 0x12;
	RTC.BCDDAY  = 0x7;
	RTC.BCDWEEK = 0x31;
	RTC.BCDHOUR = 0x23;
	RTC.BCDMIN  = 0x59;
	RTC.BCDSEC  = 0x50;
	/*禁止RTC控制*/
	RTCCON = RTCCON &  (~(1));

	while(1)
	{
		/*开始转换*/
		ADCCON = ADCCON | 1;
		/*等待转换完成*/
		while(!(ADCCON & (1 << 15)));
		/*读取转换结果*/
		AdcValue = ADCDAT & 0xFFF;
		/*将结果转换成实际的电压值mv*/
		AdcValue = AdcValue * 0.44;
		if(AdcValue >= 1501 && AdcValue <= 1801){
			//LED2 LED3 LED4 LED5均点亮
			/*点亮LED2*/
			GPX2.DAT = GPX2.DAT | (1 << 7);
			/*点亮LED3*/
			GPX1.DAT = GPX1.DAT | 1;
			/*点亮LED4*/
			GPF3.DAT = GPF3.DAT | (1 << 4);
			/*点亮LED5*/
			GPF3.DAT = GPF3.DAT | (1 << 5);
		}else if(AdcValue >= 1001 && AdcValue <= 1500){
			//LED2 LED3 LED4点亮
			/*点亮LED2*/
			GPX2.DAT = GPX2.DAT | (1 << 7);
			/*点亮LED3*/
			GPX1.DAT = GPX1.DAT | 1;
			/*点亮LED4*/
			GPF3.DAT = GPF3.DAT | (1 << 4);
			/*熄灭LED5*/
			GPF3.DAT = GPF3.DAT & (~(1 << 5));
		}else if(AdcValue >= 501 && AdcValue <= 1000){
			//LED2 LED3点亮
			/*点亮LED2*/
			GPX2.DAT = GPX2.DAT | (1 << 7);
			/*点亮LED3*/
			GPX1.DAT = GPX1.DAT | 1;
			/*熄灭LED4*/
			GPF3.DAT = GPF3.DAT & (~(1 << 4));
			/*熄灭LED5*/
			GPF3.DAT = GPF3.DAT & (~(1 << 5));
		}else {
			//LED2闪烁
			/*熄灭LED3*/
			GPX1.DAT = GPX1.DAT & (~1);
			/*熄灭LED4*/
			GPF3.DAT = GPF3.DAT & (~(1 << 4));
			/*熄灭LED5*/
			GPF3.DAT = GPF3.DAT & (~(1 << 5));
			/*点亮LED2*/
			GPX2.DAT = GPX2.DAT | (1 << 7);
			/*延时*/
			Delay(1000000);
			/*熄灭LED2*/
			GPX2.DAT = GPX2.DAT & (~(1 << 7));
			/*延时*/
			Delay(1000000);
			NewSec = RTC.BCDSEC;
			if(OldSec != NewSec)
			{
				printf("20%x-%x-%x %x %x:%x:%x ,Vol: %d\n",RTC.BCDYEAR, RTC.BCDMON, RTC.BCDWEEK, RTC.BCDDAY, RTC.BCDHOUR, RTC.BCDMIN, RTC.BCDSEC, AdcValue);	
				OldSec = NewSec;
			}
		}
	}
	return 0;
}
